using System.Collections.Generic;
using System.Net;

namespace Beetle;

public class DefaultIPFilter : IIPFiler
{
	private List<IpItem> a = new List<IpItem>();

	public void Add(IpItem item)
	{
		lock (a)
		{
			a.Add(item);
		}
	}

	public void Remove(string ipvalue, bool permit)
	{
		lock (a)
		{
			IList<IpItem> list = new List<IpItem>();
			foreach (IpItem item in a)
			{
				if (item.AddressValue == ipvalue && item.Permit == permit)
				{
					list.Add(item);
				}
			}
			foreach (IpItem item2 in list)
			{
				a.Remove(item2);
			}
		}
	}

	public void Add(string ipvalue, bool permit)
	{
		IpItem ipItem = IpItem.From(ipvalue, permit);
		if (ipItem != null)
		{
			Add(ipItem);
		}
	}

	public IpItem[] GetItems()
	{
		lock (a)
		{
			IpItem[] array = new IpItem[a.Count];
			a.CopyTo(array, 0);
			return array;
		}
	}

	public bool Execute(IPEndPoint point)
	{
		bool flag = false;
		IpItem[] items = GetItems();
		if (items.Length == 0)
		{
			return true;
		}
		IpItem[] items2 = GetItems();
		foreach (IpItem ipItem in items2)
		{
			flag = ipItem.Verification(point.Address);
			if (!flag)
			{
				break;
			}
		}
		return flag;
	}
}
